home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 16.0 KB | 586 lines | [TEXT/MPS ] |
- /*
- File: VirtualASMacFile.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __BLJSTANDARDINCLUDES__
- #include "BLJStandardIncludes.h"
- #endif
-
- #ifndef __VAPPLESINGLEMACFILE__
- #include "VirtualASMacFile.h"
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
- #ifndef __VIRTUALFILE__
- #include "VirtualFile.h"
- #endif
-
- #pragma segment VirtualAppleSingleFile
-
- /***********************************|****************************************/
-
- struct AppleSingleFileHeaderRecord
- {
- long magicNumber;
- long versionNumber;
- unsigned char homeFileSystem[16];
- unsigned short numberOfEntries;
- };
-
- /***********************************|****************************************/
-
- const unsigned long kAppleSingleMagicNumber = 0x00051600;
- const unsigned long kAppleSingleVersionNumber = 0x00010000;
-
- /***********************************|****************************************/
-
- struct AppleSingleFileEntryRecord
- {
- long entryID;
- unsigned long offset;
- unsigned long length;
- };
-
- /***********************************|****************************************/
-
- const unsigned long kAppleSingleEntryDataFork = 1;
- const unsigned long kAppleSingleEntryResourceFork = 2;
- const unsigned long kAppleSingleEntryRealName = 3;
- const unsigned long kAppleSingleEntryComment = 4;
- const unsigned long kAppleSingleEntryBWIcon = 5;
- const unsigned long kAppleSingleEntryColorIcon = 6;
- const unsigned long kAppleSingleEntryFileInfo = 7;
- const unsigned long kAppleSingleEntryFileDatesInfo = 8;
- const unsigned long kAppleSingleEntryFinderInfo = 9;
- const unsigned long kAppleSingleEntryMacintoshFileInfo = 10;
- const unsigned long kAppleSingleEntryProDOSFileInfo = 11;
- const unsigned long kAppleSingleEntryMSDOSFileInfo = 12;
- const unsigned long kAppleSingleEntryAFPShortName = 13;
- const unsigned long kAppleSingleEntryAFPFileInfo = 14;
- const unsigned long kAppleSingleEntryAFPDirectoryID = 15;
-
- /***********************************|****************************************/
-
- static OSErr vFSReadFrom (TVirtualFile *vF, long offset, long& count, void *data) {
- if (vF)
- {
- vF->SetPosition (fsFromStart, offset);
- return vF->ReadData (data, count);
- }
- return paramErr;
- }
-
- /***********************************|****************************************/
-
- #if debug
- Str255& DateToStr ( unsigned long secs )
- {
- static Str255 result;
- Str255 temp1;
-
- IUDateString ( secs, shortDate, result );
-
- IUTimeString ( secs, true, temp1 );
- PLstrcat ( result, "\p ");
- PLstrcat ( result, temp1 );
-
- return result;
- }
- #endif
-
- /***********************************|****************************************/
-
- TVirtualAppleSingleMacFile::TVirtualAppleSingleMacFile (TVirtualFile *baseFile)
- {
- VOLATILE(baseFile);
-
- TRY
- fAppleSingleBaseFile = nil;
- fDataForkOffset = fDataForkLength = fDataForkFPos = 0;
- fResourceForkOffset = fResourceForkLength = fResourceForkFPos = 0;
-
- FAILNULL (baseFile);
-
- long fileSize = 0;
- FAILOSErr (baseFile->GetEnd (fileSize));
- FAILOSErr(baseFile->SetPosition (fsFromStart, 0));
-
- #if debug
- if (keithFlag.Flag(12)) {
- keith << "TVirtualAppleSingleMacFile:: baseFile size=" << fileSize << endl << flush;
- keith << "FILE DATA:" << endl;
-
- {
- Ptr data = FAILNewPtr(fileSize);
- if (data) {
- long dataSize = fileSize;
- vFSReadFrom(baseFile, 0, dataSize, (void*) data);
- DumpHex(keith, (char*) data, dataSize);
- }
-
- DeallocatePtr((Ptr) data);
- }
- }
- #endif
-
- // Verify that there is an AppleSingle file header on this file.
- AppleSingleFileHeaderRecord appleSingleFileHeader;
- long dataSize = sizeof(appleSingleFileHeader);
- long offset = 0;
- FAILOSErr ( vFSReadFrom (baseFile, offset, dataSize, &appleSingleFileHeader));
-
- if (appleSingleFileHeader.magicNumber != kAppleSingleMagicNumber) {
- FAIL (-1);
- }
-
- offset += sizeof(appleSingleFileHeader);
-
- // OK. The file has what appears to be an AppleSingle header. Loop through,
- // reading each file entry record.
- for (unsigned short entriesLeft = appleSingleFileHeader.numberOfEntries ; entriesLeft; --entriesLeft) {
- AppleSingleFileEntryRecord fileEntry;
- long dataSize = sizeof(fileEntry);
-
- FAILOSErr (vFSReadFrom (baseFile, offset, dataSize, &fileEntry));
-
- switch (fileEntry.entryID) {
- case kAppleSingleEntryDataFork:
- fDataForkOffset = fileEntry.offset;
- fDataForkLength = fileEntry.length;
-
- keithF( 21, "AppleSingle: fDataForkOffset=" << fDataForkOffset << " length=" << fDataForkLength );
- break;
-
- case kAppleSingleEntryResourceFork:
- fResourceForkOffset = fileEntry.offset;
- fResourceForkLength = fileEntry.length;
-
- keithF( 21, "AppleSingle: fResourceForkOffset=" << fResourceForkOffset << " length=" << fResourceForkLength );
- break;
-
- case kAppleSingleEntryRealName: {
- long dataSize = shortmin(sizeof(fFileName), fileEntry.length);
- FAILOSErr (vFSReadFrom (baseFile, fileEntry.offset, dataSize, &fFileName[0]));
-
- keithF( 21, "AppleSingle: realFileName=" << fFileName );
-
- #if debug
- if (fFileName[0] + 1 != dataSize) {
- keith << "AppleSingle: REAL FILE NAME DOES NOT APPEAR TO BE A STR32! ("
- << dataSize << " vs. " << (short) fFileName[0] + 1 << ")" << endl << flush;
- }
- #endif
- break;
- }
-
- case kAppleSingleEntryFinderInfo: {
- long dataSize = shortmin(sizeof(fFinderInfo), fileEntry.length);
- FAILOSErr (vFSReadFrom (baseFile, fileEntry.offset, dataSize, &fFinderInfo));
-
- keithF( 21, "AppleSingle: fFinderInfo, type="; OutputOSType(keith, fFinderInfo.fdType); keith << " creator="; OutputOSType(keith, fFinderInfo.fdCreator); keith << " flags=" << fFinderInfo.fdFlags );
- break;
- }
-
- case kAppleSingleEntryComment:
- case kAppleSingleEntryBWIcon:
- case kAppleSingleEntryColorIcon:
- default:
- keithF( 21, "AppleSingle: entry ignored #" << fileEntry.entryID << " @ offset=" << offset << " length=" << fileEntry.length );
- break;
-
- case kAppleSingleEntryFileInfo:
- {
- typedef struct AppleSingleVersionOneFileDatesInfoStruct {
- unsigned long creationDate;
- unsigned long modificationDate;
- unsigned long backupDate;
- unsigned long accessDate;
- } AppleSingleVersionOneFileDatesInfoStruct;
-
- if ( fileEntry.length >= sizeof( AppleSingleVersionOneFileDatesInfoStruct ) )
- { AppleSingleVersionOneFileDatesInfoStruct relevantDates;
- long dataSize = sizeof(relevantDates);
-
- FAILOSErr (vFSReadFrom (baseFile, fileEntry.offset, dataSize, &relevantDates));
-
- fCreationDate = relevantDates.creationDate;
- fModificationDate = relevantDates.modificationDate;
- fBackupDate = relevantDates.backupDate;
- }
- else
- {
- fCreationDate = NowDateTime();
- fModificationDate = fCreationDate;
- fBackupDate = 0;
- }
-
- keithF( 12, "AppleSingle: (Version 1) creation=" << DateToStr( fCreationDate ) <<
- " mod=" << DateToStr ( fModificationDate ) );
- break;
- }
-
- case kAppleSingleEntryFileDatesInfo:
- {
- typedef struct AppleSingleFileDatesInfoStruct {
- signed long creationDate;
- signed long modificationDate;
- signed long backupDate;
- signed long accessDate;
- } AppleSingleFileDatesInfoStruct;
-
- signed long kDeltaBetweenAppleSingleDateAndMacintoshDate = - 3029529600;
-
- if ( fileEntry.length >= sizeof( AppleSingleFileDatesInfoStruct ) )
- { AppleSingleFileDatesInfoStruct relevantDates;
- long dataSize = sizeof(relevantDates);
-
- FAILOSErr (vFSReadFrom (baseFile, fileEntry.offset, dataSize, &relevantDates));
-
- fCreationDate = relevantDates.creationDate + kDeltaBetweenAppleSingleDateAndMacintoshDate;
- fModificationDate = relevantDates.modificationDate + kDeltaBetweenAppleSingleDateAndMacintoshDate;
- fBackupDate = relevantDates.backupDate + kDeltaBetweenAppleSingleDateAndMacintoshDate;
- }
- else
- {
- fCreationDate = NowDateTime();
- fModificationDate = fCreationDate;
- fBackupDate = 0;
- }
-
- keithF ( 12, "AppleSingle: creation=" << DateToStr( fCreationDate ) <<
- " mod=" << DateToStr ( fModificationDate ) );
- break;
- }
-
- case kAppleSingleEntryMacintoshFileInfo:
- case kAppleSingleEntryProDOSFileInfo:
- case kAppleSingleEntryMSDOSFileInfo:
- case kAppleSingleEntryAFPShortName:
- case kAppleSingleEntryAFPFileInfo:
- case kAppleSingleEntryAFPDirectoryID:
- break;
-
- }
-
- offset += sizeof(fileEntry);
- }
-
- fAppleSingleBaseFile = baseFile;
-
- EXCEPTION
- fAppleSingleBaseFile = nil; // signals an error
-
- ENDEXCEPTION
-
- if (fAppleSingleBaseFile != nil)
- fAppleSingleBaseFile->RegisterReference();
- }
-
- /***********************************|****************************************/
-
- TVirtualAppleSingleMacFile::~TVirtualAppleSingleMacFile() {
- if (fAppleSingleBaseFile != nil) {
- fAppleSingleBaseFile->UnregisterReference();
- }
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::Open () {
- if (fAppleSingleBaseFile)
- return noErr;
-
- return fnfErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::Close () {
-
- fDataForkFPos = 0;
- fResourceForkFPos = 0;
-
- if (fAppleSingleBaseFile)
- return noErr;
-
- return fnfErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::ReadData (void* buffer,long& count,TVirtualMacFile::ForkType whichFork) {
- OSErr result = noErr;
- long offset;
-
- if (fAppleSingleBaseFile == nil) {
- keithF ( 12, "TVirtualASF::MReadData, bad apple single file" );
- return fnfErr;
- }
-
- switch (whichFork) {
- case TVirtualMacFile::kData:
- offset = fDataForkFPos + fDataForkOffset;
- count = longmin (fDataForkLength - fDataForkFPos, count);
- break;
-
- case TVirtualMacFile::kResource:
- offset = fResourceForkFPos + fResourceForkOffset;
- count = longmin(fResourceForkLength - fResourceForkFPos, count);
- break;
-
- default:
- keithF( 12, "TVirtualASF::MReadData, bad fork type" );
- return fnfErr;
- }
-
- if ((result = fAppleSingleBaseFile->SetPosition (fsFromStart, offset)) == noErr) {
- result = fAppleSingleBaseFile->ReadData (buffer, count);
- }
-
- switch (whichFork) {
- case TVirtualMacFile::kData:
- fDataForkFPos += count;
- break;
- case TVirtualMacFile::kResource:
- fResourceForkFPos += count;
- break;
- }
-
- keithF( 21, "TVirtualASF::MReadData, result=" << result << " offset=" << offset << " count=" << count );
-
- return result;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::WriteData (const void* buffer, long& count, TVirtualMacFile::ForkType whichFork) {
- unused (buffer);
- unused (count);
- unused (whichFork);
-
- // For the time being, don't allow writes. Eventually, this should be revisited
- // (especially if we 'genericize' this class for public use)
- return wrPermErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::SetEnd (long logEof, TVirtualMacFile::ForkType whichFork) {
- unused (logEof);
- unused (whichFork);
-
- return wrPermErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::GetEnd (long& logEof, TVirtualMacFile::ForkType whichFork) const {
- if (fAppleSingleBaseFile == nil) {
- keithF( 21, "TVirtualASF::MGetEOF, bad apple single file" );
- return paramErr;
- }
-
- switch (whichFork) {
- case TVirtualMacFile::kData:
- logEof = fDataForkLength;
- keithF( 21, "TVirtualASF::MGetEOF, data forkSize=" << logEof );
- return noErr;
-
- case TVirtualMacFile::kResource:
- logEof = fResourceForkLength;
- keithF( 21, "TVirtualASF::MGetEOF, resource forkSize=" << logEof );
- return noErr;
- }
-
- keithF( 21, "TVirtualASF::MGetEOF, bad fork type" );
-
- logEof = 0;
- return paramErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::SetPosition (short posMode, long posOffset, TVirtualMacFile::ForkType whichFork) {
-
- if (fAppleSingleBaseFile == nil) {
- keithF( 21, "TVirtualASF::MSetFPos, bad apple single file." );
- return fnfErr;
- }
-
- OSErr result = paramErr;
- switch (whichFork) {
- case TVirtualMacFile::kData:
- switch (posMode) {
- case fsFromStart:
- fDataForkFPos = longmax(0, longmin(fDataForkLength, posOffset));
- result = noErr;
- break;
-
- case fsFromLEOF:
- fDataForkFPos = longmax(0, longmin(fDataForkLength, fDataForkLength - posOffset));
- result = noErr;
- break;
-
- case fsFromMark:
- fDataForkFPos = longmax(0, longmin(fDataForkLength, fDataForkFPos + posOffset));
- result = noErr;
- break;
-
- default:
- result = paramErr;
- break;
- }
-
- case TVirtualMacFile::kResource:
- switch (posMode) {
- case fsFromStart:
- fResourceForkFPos = longmax(0, longmin(fResourceForkLength, posOffset));
- result = noErr;
- break;
-
- case fsFromLEOF:
- fResourceForkFPos = longmax(0, longmin(fResourceForkLength, fResourceForkLength - posOffset));
- result = noErr;
- break;
-
- case fsFromMark:
- fResourceForkFPos = longmax(0, longmin(fResourceForkLength, fResourceForkFPos + posOffset));
- result = noErr;
- break;
-
- default:
- result = paramErr;
- break;
- }
- }
-
- keithF( 21, "TVirtualASF::MSetFPos, err=" << result << " fDataPos=" << fDataForkFPos << " fResPos=" << fResourceForkFPos );
-
- return result;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::GetPosition (long& filePos,TVirtualMacFile::ForkType whichFork) const {
- if (fAppleSingleBaseFile == nil) {
- keithF( 21, "TVirtualASF::MGetFPos, bad apple single file." );
- return fnfErr;
- }
-
- OSErr result = paramErr;
-
- switch (whichFork) {
- case TVirtualMacFile::kData:
- filePos = fDataForkFPos;
- result = noErr;
- break;
-
- case TVirtualMacFile::kResource:
- filePos = fResourceForkFPos;
- result = noErr;
- break;
- }
-
- keithF( 21, "TVirtualASF::MGetFPos, fork " << (short) whichFork << " pos=" << filePos );
-
- return result;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::SetFinderInfo (const FInfo& finderInfo) {
- unused (finderInfo);
-
- return wrPermErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::GetFinderInfo (FInfo& finderInfo) const {
- finderInfo = fFinderInfo;
-
- return noErr;
- }
-
- /***********************************|****************************************/
-
- void TVirtualAppleSingleMacFile::SetUserRef(long ref) {
- fUserRefNum = ref;
- }
-
- /***********************************|****************************************/
-
- long TVirtualAppleSingleMacFile::GetUserRef() const{
- return fUserRefNum;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::GetDate (unsigned long& dateTime, TVirtualMacFile::WhichDateType whichDate) const {
- OSErr result = noErr;
-
- if ( whichDate == kCreationDate )
- dateTime = fCreationDate;
- else if ( whichDate == kModDate )
- dateTime = fModificationDate;
- else
- {
- dateTime = 0;
- result = paramErr;
- }
-
- return result;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::SetDate (unsigned long dateTime, TVirtualMacFile::WhichDateType whichDate) {
- unused(whichDate);
-
- return wrPermErr;
- }
-
- /***********************************|****************************************/
-
- Boolean TVirtualAppleSingleMacFile::IsValidAppleSingleFile() const
- {
- return (fAppleSingleBaseFile != nil);
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::SetSpec ( const FSSpec& spec )
- {
- PLstrcpy ( fFileName, spec.name );
- return noErr;
- }
-
- /***********************************|****************************************/
-
- OSErr TVirtualAppleSingleMacFile::GetSpec ( FSSpec& spec ) const
- {
- PLstrcpy ( spec.name, fFileName );
- return noErr;
- }
-
- /***********************************|****************************************/